
(function($) {

    'use strict';

    $.extend($.fn.bootstrapTable.defaults, {
        previewfile: true,
    });

    var BootstrapTable = $.fn.bootstrapTable.Constructor,
        _initTable = BootstrapTable.prototype.initTable,
        _initBody = BootstrapTable.prototype.initBody;

    BootstrapTable.prototype.initTable = function() {
        _initTable.apply(this, Array.prototype.slice.apply(arguments));

        if (!this.options.previewfile) {
            return;
        }

        $.each(this.columns, function(i, column) {
            if (!column.previewfile) {
                return;
            }
            column.formatter = function(value, row, index) {
                var result = column._formatter ? column._formatter(value, row, index) : value;

                var tpl = '';
                var video_url_array = result.split(',');
                video_url_array.forEach(function(url){
                    var ext = url.slice(url.lastIndexOf(".")+1).toLowerCase();
                    if(ext.lastIndexOf("?") > -1){
                        ext = ext.slice(0,ext.lastIndexOf("?")).toLowerCase();
                    }
                    var img = '/assets/addons/previewfile/image/file.png';
                    var title = '文件预览';
                    if(ext == 'docx' || ext == 'doc') {
                        img = '/assets/addons/previewfile/image/word.png';
                        title = 'Word文档预览';
                    }else if(ext == 'xlsx' || ext == 'xls' || ext == 'csv'){
                        img = '/assets/addons/previewfile/image/excel.png';
                        title = 'Execl文档预览';
                    }else if(ext == 'pdf'){
                        img = '/assets/addons/previewfile/image/pdf.png';
                        title = 'PDF文件预览';
                    }else if(ext == 'ppt' || ext == 'pptx'){
                        img = '/assets/addons/previewfile/image/pptx.png';
                        title = 'PPT文件预览';
                    }else if(ext == 'mp4'){
                        img = '/assets/addons/previewfile/image/video.png';
                        title = '视频播放';
                    }
                    tpl += '<a href="javascript:void(0)"'+
                        ' data-name="' + column.field + '"'+
                        ' data-title="' + title + '"'+
                        ' data-url="' + url + '"  >'+
                        '<img src="'+img+'" class="img-sm img-center" ></a>';
                })

                return [tpl].join('');

            };
        });
    };

    BootstrapTable.prototype.initBody = function() {
        var that = this;
        _initBody.apply(this, Array.prototype.slice.apply(arguments));

        if (!this.options.previewfile) {
            return;
        }

        $.each(this.columns, function(i, column) {
            if (!column.previewfile) {
                return;
            }
            that.$body.find('a[data-name="' + column.field + '"]').click(function(v){
                var url = $(this).data('url');
                var title = $(this).data('title');
                window.Fast.api.open('/addons/previewfile/index?url='+url, title,{
                    callback:function(value){

                    }
                })
            })
        });
    };

})(jQuery);
